import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/data/entities/application/execute_entity.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';

///特殊作业-关联的其他特殊作业及安全作业票编号
///[onCallBack] 选择回调
///[isRequired] 是否必填
///[isEnabled] 是否可编辑
///[isShowDivider] 是否显示分割线
///[selectedWork] 已选择的作业票
class CustomSelectRelationPage extends StatefulWidget {
  const CustomSelectRelationPage({
    required this.onCallBack,
    super.key,
    this.isRequired,
    this.isEnabled = true,
    this.isShowDivider = true,
    this.selectedWork,
  });

  final Function(List<String>) onCallBack;
  final bool? isRequired;
  final bool isEnabled;
  final bool isShowDivider;
  final List<String>? selectedWork;

  @override
  State<CustomSelectRelationPage> createState() =>
      _CustomSelectRelationPageState();
}

class _CustomSelectRelationPageState extends State<CustomSelectRelationPage> {
  List<String> selectedList = []; // 选中的列表

  @override
  initState() {
    super.initState();
    _initDate();
  }

  /// 初始化数据
  void _initDate() {
    if (widget.selectedWork != null && widget.selectedWork!.isNotEmpty) {
      selectedList.addAll(widget.selectedWork!);
    }
  }

  ///获取文件列显示
  Widget _getWorkView(String name) {
    return Row(
      children: [
        Expanded(
            child: Text(
          name,
          style: TextStyle(
            fontSize: 13.sp,
            color: const Color(0xFF5C6DFF),
          ),
        )),
        Visibility(
            visible: widget.isEnabled,
            child: Container(
              alignment: Alignment.center,
              child: const Icon(
                Icons.highlight_remove,
                color: Color(0xFFFD6B6B),
              ),
            ).clickOnce(() {
              setState(() {
                selectedList.remove(name);
              });
              _callBack();
            }))
      ],
    );
  }

  ///回调给外部
  void _callBack() {
    widget.onCallBack(selectedList);
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        AppGaps.vGap20,
        Text(
          '关联的其他特殊作业及安全作业票编号',
          style: TextStyle(
            fontSize: 14.sp,
            height: 1.1,
            color: AppColors.font_color_black,
          ),
        ),
        AppGaps.vGap10,
        Visibility(
            visible: widget.isEnabled,
            child: Column(
              children: [
                Container(
                    height: 32.w,
                    padding: EdgeInsets.symmetric(horizontal: 12.w),
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                        color: Colors.white,
                        border: Border.all(
                            color: const Color(0xffF0F0F0), width: 1.w),
                        borderRadius: BorderRadius.all(Radius.circular(8.w))),
                    child: Row(
                      children: [
                        SvgPicture.asset(
                          AppImages.icon_search,
                          width: 16.w,
                        ),
                        AppGaps.hGap8,
                        Text(
                          '请搜索',
                          style: TextStyle(
                              fontSize: 14.sp,
                              fontWeight: FontWeight.w400,
                              color: const Color(0xffA0A7B5)),
                        ),
                      ],
                    )).clickOnce(() {
                  Get.toNamed(AppRoutes.relatedTicketSelection, arguments: {
                    'isRadio': false,
                  })?.then((value) {
                    //如果不为空则设置数据
                    if (value != null && value is List<ExecuteTicketReceiptList>) {
                      // 过滤掉id字段相同的数据
                      final List<String> filteredList = [];
                      for (var item in value) {
                        filteredList.add(item.getShowName());
                      }
                      selectedList.addAll(filteredList
                          .toSet()
                          .difference(selectedList.toSet()));
                      setState(() {});
                      _callBack();
                    }
                  });
                }),
                AppGaps.vGap14,
              ],
            )),
        ListView.separated(
          itemCount: selectedList.length,
          shrinkWrap: true,
          physics: const NeverScrollableScrollPhysics(),
          itemBuilder: (context, index) {
            return _getWorkView(selectedList[index]);
          },
          separatorBuilder: (BuildContext context, int index) {
            return AppGaps.vGap8;
          },
        ),
        Visibility(visible: selectedList.isNotEmpty, child: AppGaps.vGap10),
        Visibility(
            visible: widget.isShowDivider,
            child: Divider(
              height: 1.w,
              color: AppColors.bg_color_grey,
            )),
      ],
    );
  }
}
